package com.zhouyu.gamecommunity.config;

import com.zhouyu.gamecommunity.utils.UDPUtil;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class InitService {

    private static final int PORT = 8088;

    @PostConstruct
    public void initUDP() {
        // 初始化UDP服务
        if (UDPUtil.isPortAvailable(PORT)) {
            UDPUtil.startReceiveLoop(PORT,(sender,message)->{
                // 处理接收到的消息
                System.out.println("收到来自" + sender + "的消息：" + message);
                try {
                    UDPUtil.send(sender.split(":")[0], Integer.parseInt(sender.split(":")[1]), "Hello, UDP!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
